const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {
    entries,
    pages,
    chunks
} = require('./view.config');


// console.log(process.env.NODE_ENV == "production");
// process.env.NODE_ENV = "production";

module.exports = {
    context: path.resolve(__dirname, '../'),
    entry: entries,
    output: {
        path: path.resolve(__dirname, '../dist'),
        publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
        filename: '[name].[hash:8].js'
    },
    module: {
        rules: [{
                test: /\.js$/,
                exclude: /node_modules/,
                use: ['babel-loader']
            },
            {
                test: /\.ejs$/,
                exclude: /node_modules/,
                use: ['ejs-loader']
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                use: [
                    process.env.NODE_ENV !== 'production' ?
                    'style-loader' :
                    MiniCssExtractPlugin.loader,
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            sourceMap: true
                        }
                    },
                    {
                        loader: 'sass-loader',
                        options: {
                            sourceMap: true
                        }
                    }
                ]
            },
            {
                test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
                exclude: /node_modules/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 512
                    }
                }]
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            isProduction: process.env.NODE_ENV === 'production'
        }),
        ...Object.keys(pages).map(k => {
            return new HtmlWebpackPlugin({
                template: k,
                filename: pages[k] + '.html',
                chunks: chunks[[pages[k]]],
                inject: false,
                minify: process.env.NODE_ENV !== 'production' ?
                    false : {
                        collapseWhitespace: true,
                        minifyJS: true,
                        removeComments: true
                    }
            });
        }),
        new CopyWebpackPlugin([{
                from: './node_modules/jquery/dist/jquery.min.js',
                to: 'libs/[name].[ext]'
            },
            {
                from: './libs/**/*',
                to: 'libs/[name].[ext]'
            },
            {
                from: './assets/**/*',
                to: ''
            }
        ])
    ],
    externals: {
        jquery: 'jQuery'
    }
};